package com.cn.wanxi.servlet.product;

import com.alibaba.fastjson.JSONObject;
import com.cn.wanxi.model.PageModel;
import com.cn.wanxi.model.ProductModel;
import com.cn.wanxi.service.IProductService;
import com.cn.wanxi.service.impl.ProductServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/back/product/findAll")
public class ProductFindAllServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

//        乱码
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");

        String cookTypeId = req.getParameter("cookTypeId");
        String productName = req.getParameter("productName");
        String startNormalPrice = req.getParameter("startNormalPrice");
        String endNormalPrice = req.getParameter("endNormalPrice");
        String productDiscrib = req.getParameter("productDiscrib");
        String enable = req.getParameter("enable");
        String pageNumString = req.getParameter("pageNum");
        String pageSizeString = req.getParameter("pageSize");
        Integer pageNum = "".equals(pageNumString) ? 1 : Integer.parseInt(pageNumString);
        Integer pageSize = "".equals(pageSizeString) ? 10 : Integer.parseInt(pageSizeString);


        ProductModel productModel = new ProductModel();

        productModel.setCookTypeId("".equals(cookTypeId) ? 0 : Integer.parseInt(cookTypeId));
        productModel.setName(productName);
        productModel.setStartNormalPrice("".equals(startNormalPrice) ? 0.0 : Double.parseDouble(startNormalPrice));
        productModel.setEnable("".equals(enable) ? 0 : Integer.parseInt(enable));
        productModel.setEndNormalPrice("".equals(endNormalPrice) ? 0.0 : Double.parseDouble(endNormalPrice));
        productModel.setDiscrib(productDiscrib);

        productModel.setPageModel(new PageModel(pageNum, pageSize));


        IProductService iProductService = new ProductServiceImpl();
        List<ProductModel> list = iProductService.findAll(productModel);
        int count = iProductService.getCount(productModel);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list", list);
        jsonObject.put("count", count);
        resp.getWriter().println(jsonObject);
    }
}
